package com.gexin.weixin.controller.base;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.model.ApiResponse;
import com.gexin.model.weixin.WeixinUser;
import com.gexin.weixin.kit.WeXinCacheKeyConst;
import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;

public class WxTokenBaseController extends Controller {

	/**
	 * 获取当前授权的 微信用户的 openid
	 * @return
	 */
	@NotAction
	public String getLoginUserOpenid() {
		return getLoginUser().getOpenid();
	}

	/**
	 * 获取当前登录的用户id
	 *
	 * @return
	 */
	@NotAction
	public Integer getLoginUserId() {
		return getLoginUser().getId();
	}

	/**
	 * 获取当前登录用户的详细信息
	 *
	 * @return
	 */
	@NotAction
	public WeixinUser getLoginUser() {
		return CacheUtils.get(WeXinCacheKeyConst.wx_token + getAttrForStr("wx_token"));
	}

	@NotAction
	public void success() {
		renderJson(new ApiResponse(true));
	}

	@NotAction
	public void success(String msg) {
		renderJson(new ApiResponse(true, msg));
	}

	@NotAction
	public void success(Object data) {
		renderJson(new ApiResponse(data));
	}

	@NotAction
	public void fail() {
		renderJson(new ApiResponse(false));
	}

	@NotAction
	public void fail(String msg) {
		renderJson(new ApiResponse(false, msg));
	}

	@NotAction
	public void state(boolean state) {
		renderJson(new ApiResponse(state));
	}


}
